home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 2238 < prev    next >
Encoding:
Text File  |  1996-08-06  |  1.8 KB  |  54 lines

  1. Path: ix.netcom.com!netnews
  2. From: miker3@ix.netcom.com (Mike Rubenstein)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Q:order of evaluation
  5. Date: Tue, 16 Jan 1996 13:15:18 GMT
  6. Organization: Netcom
  7. Message-ID: <30fba2c3.37871680@nntp.ix.netcom.com>
  8. References: <4dfhlu$a33$1@mhafn.production.compuserve.com>
  9. NNTP-Posting-Host: ix-dc6-24.ix.netcom.com
  10. X-NETCOM-Date: Tue Jan 16  5:15:09 AM PST 1996
  11. X-Newsreader: Forte Agent .99c/16.141
  12.  
  13. Holger Maier <100336.3326@CompuServe.COM> wrote:
  14.  
  15. |>Consider
  16. |>#include <iostream>
  17. |>int main() {
  18. |>  int i=1;int j=i+(i+=1);
  19. |>  cout<<i<<','<<j<<'\n';
  20. |>  return 0;
  21. |>}
  22. |>on my compiler this produces 2,4
  23. |>Looked up the ARM:
  24. |>5: .. The order of evaluation of subexpressions is determined by the
  25. |>precedence and grouping of operators.
  26. |>5.7: The additive operators + and - group left to right.
  27. |>So, j should be assigned 3 ??
  28. |>Now the question to you C++ gurus out there on the nets:
  29. |>Is it really a compiler bug or is it just me misinterpreting the 
  30. |>ARM?
  31.  
  32. You're misinterpretting the ARM.  The exact quote from my copy is
  33.  
  34.     The order of evaluation of subexpressions is determined by the
  35.  
  36.     expression grammar.  The usual mathematical rules for 
  37.     associativity or operators may be applied only where the 
  38.     operators really are associative and commutabtive.  Except 
  39.     where noted, the order of evaluation of operands of individual
  40.  
  41.     operators is undefined.
  42.  
  43. In the expression A + B, A and B must be evaluated before the sum, but
  44. there is no requirement as to the order in which they are evaluated.
  45.  
  46. Note that the draft changes this.  Not only is the order of evaluation
  47. undefined, but in situations like you show in which a variable is
  48. modified and it's value used, other than to determine the new value,
  49. the behavior is undefined.  The compiler need not produce code that
  50. evaluates the expression at all -- it could do anything.
  51.  
  52.  
  53. Michael M Rubenstein
  54.